package mysql

import (
	"errors"
	"fmt"
)

type Admins struct {
	Id         uint `gorm:"column:id;primary_key" json:"id" form:"id"`
	Status     int  `gorm:"column:status" json:"status" form:"status"`
	Deleted    int  `gorm:"column:deleted" json:"deleted"`
	AddTime    int32 `gorm:"column:add_time" json:"add_time"`
	UpdateTime int32 `gorm:"column:update_time" json:"update_time"`
	Memo       string `gorm:"column:memo" json:"memo" form:"memo"`
	Name     string `gorm:"column:name" json:"name"`
	Account  string `gorm:"column:account" json:"account"`
	Password string `gorm:"column:password" json:"password"`
	Acode    string `gorm:"column:acode" json:"acode"`
}
//这一步的意思是改表明的  默认表明是带s的
func (Admins) TableName() string  {
	return "k_admin"
}

func (model Admins) GetList(where []map[string]interface{},page int,pageSize int) (map[string]interface{},error){

	if page <= 0 {
		return nil,errors.New("page 不正确")
	}
	if page == 1 {
		page = 0
	}
	result := map[string]interface{}{}
	db :=Db
	num := 0
	adminsArray := []Admins{}
	offset := (page-1)*pageSize


	for _, m := range where {
		db = db.Where(m["filed"].(string)+" "+m["exp"].(string)+" ?",m["val"])
	}

	err := db.Model(&Admins{}).Count(&num).Limit(pageSize).Offset(offset).Find(&adminsArray).Error
	if err == nil{
		result["data"] = adminsArray
		result["count"] = num
	}
	return result,err;

}


func (model Admins) GetAdminByAccount(account string) (Admins,error){
	var admins Admins
	err := Db.Where("account = ?",account).Find(&admins).Error
	if err != nil {
		fmt.Println("查询错误",err)
        return admins,err
	}
    return admins,err
}

func (model Admins) Sort(id int,px int)error{
	db := Db
	Admins:=Admins{}
	err := db.Model(&Admins).Where("id = ?",id).Update("px",px).Error
	return err
}

func (model Admins) SetStatus(id int,status int)error{
	db := Db
	Admins:=Admins{}
	err := db.Model(&Admins).Where("id = ?",id).Update("status",status).Error
	return err
}

func (n Admins) Add(Admins Admins)error{
	db := Db
	err := db.Create(&Admins).Error
	return err
}

func (model Admins) Save(Admins Admins)error{
	db := Db
	err := db.Save(&Admins).Error
	return err
}

func (model Admins) Del(id int) error {
	db :=Db
	Admins := Admins{}
	err := db.Where("id = ?",id).Delete(&Admins).Error
	return err
}

func (model Admins) GetItemById(id int) (Admins,error){

	db := Db
	Admins := Admins{}
	err = db.Find(&Admins,id).Error
	if err != nil {
		fmt.Println("查询错误", err)
		return Admins, err
	}
	return Admins, err
}




